/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.Instance;
/*     */ import com.zimbra.cs.mailbox.Document;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.InviteInfo;
/*     */ import com.zimbra.cs.mailbox.calendar.ZOrganizer;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import java.io.IOException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Collection;
/*     */ import java.util.Date;
/*     */ import java.util.Iterator;
/*     */ import javax.servlet.ServletOutputStream;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class RssFormatter
/*     */   extends Formatter
/*     */ {
/*  40 */   protected SimpleDateFormat mDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
/*     */   
/*     */ 
/*     */   public void formatCallback(UserServletContext context)
/*     */     throws IOException, ServiceException
/*     */   {
/*  46 */     Iterator<? extends MailItem> iterator = null;
/*  47 */     StringBuffer sb = new StringBuffer();
/*  48 */     Element.XMLElement rss = new Element.XMLElement("rss");
/*  49 */     int offset = context.getOffset();
/*  50 */     int limit = context.getLimit();
/*     */     try
/*     */     {
/*  53 */       iterator = getMailItems(context, context.getStartTime(), context.getEndTime(), limit - offset);
/*     */       
/*  55 */       context.resp.setCharacterEncoding("UTF-8");
/*  56 */       context.resp.setContentType("application/rss+xml");
/*     */       
/*  58 */       sb.append("<?xml version=\"1.0\"?>\n");
/*     */       
/*  60 */       rss.addAttribute("version", "2.0");
/*  61 */       Element channel = rss.addElement("channel");
/*  62 */       channel.addElement("title").setText("Zimbra " + context.itemPath);
/*  63 */       channel.addElement("link").setText("http://www.zimbra.com");
/*  64 */       channel.addElement("description").setText("Zimbra item " + context.itemPath + " in RSS format.");
/*  65 */       channel.addElement("generator").setText("Zimbra RSS Feed Servlet");
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*  70 */       int curHit = 0;
/*     */       
/*  72 */       while (iterator.hasNext()) {
/*  73 */         MailItem itItem = (MailItem)iterator.next();
/*  74 */         curHit++;
/*  75 */         if (curHit > limit)
/*     */           break;
/*  77 */         if (curHit >= offset) {
/*  78 */           if ((itItem instanceof CalendarItem))
/*     */           {
/*  80 */             CalendarItem calItem = (CalendarItem)itItem;
/*  81 */             if ((calItem.isPublic()) || (calItem.allowPrivateAccess(context.getAuthAccount(), context.isUsingAdminPrivileges())))
/*     */             {
/*  83 */               addCalendarItem(calItem, channel, context);
/*     */             }
/*  85 */           } else if ((itItem instanceof Message)) {
/*  86 */             addMessage((Message)itItem, channel);
/*  87 */           } else if ((itItem instanceof Document)) {
/*  88 */             addDocument((Document)itItem, channel, context);
/*     */           }
/*     */         }
/*     */       }
/*     */     } finally {
/*  93 */       if ((iterator instanceof Formatter.QueryResultIterator))
/*  94 */         ((Formatter.QueryResultIterator)iterator).finished();
/*     */     }
/*  96 */     sb.append(rss.toString());
/*  97 */     context.resp.getOutputStream().write(sb.toString().getBytes("UTF-8"));
/*     */   }
/*     */   
/*     */   public long getDefaultStartTime()
/*     */   {
/* 102 */     return System.currentTimeMillis() - 604800000L;
/*     */   }
/*     */   
/*     */ 
/*     */   public long getDefaultEndTime()
/*     */   {
/* 108 */     return System.currentTimeMillis() + 604800000L;
/*     */   }
/*     */   
/*     */   private void addCalendarItem(CalendarItem calItem, Element channel, UserServletContext context) throws ServiceException {
/* 112 */     Collection<CalendarItem.Instance> instances = calItem.expandInstances(context.getStartTime(), context.getEndTime(), false);
/* 113 */     for (Iterator<CalendarItem.Instance> instIt = instances.iterator(); instIt.hasNext();) {
/* 114 */       CalendarItem.Instance inst = (CalendarItem.Instance)instIt.next();
/* 115 */       InviteInfo invId = inst.getInviteInfo();
/* 116 */       Invite inv = calItem.getInvite(invId.getMsgId(), invId.getComponentId());
/* 117 */       Element rssItem = channel.addElement("item");
/* 118 */       rssItem.addElement("title").setText(inv.getName());
/* 119 */       rssItem.addElement("pubDate").setText(this.mDateFormat.format(new Date(inst.getStart())));
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 128 */       rssItem.addElement("description").setText(inv.getFragment());
/* 129 */       if (inv.hasOrganizer()) {
/* 130 */         rssItem.addElement("author").setText(inv.getOrganizer().getAddress());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void addMessage(Message m, Element channel) {
/* 136 */     Element item = channel.addElement("item");
/* 137 */     item.addElement("title").setText(m.getSubject());
/* 138 */     item.addElement("description").setText(m.getFragment());
/* 139 */     item.addElement("author").setText(m.getSender());
/* 140 */     item.addElement("pubDate").setText(this.mDateFormat.format(new Date(m.getDate())));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private void addDocument(Document doc, Element channel, UserServletContext context)
/*     */   {
/* 148 */     Element item = channel.addElement("item");
/* 149 */     item.addElement("title").setText(doc.getName() + " ver " + doc.getVersion());
/* 150 */     item.addElement("description").setText(doc.getFragment());
/* 151 */     item.addElement("author").setText(doc.getCreator());
/* 152 */     item.addElement("pubDate").setText(this.mDateFormat.format(new Date(doc.getDate())));
/* 153 */     item.addElement("link").setText(new StringBuilder().append("?id=").append(doc.getId()).toString());
/*     */   }
/*     */   
/*     */   public FormatterFactory.FormatType getType()
/*     */   {
/* 158 */     return FormatterFactory.FormatType.RSS;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/RssFormatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */